package com.bluegalaxy.nhb.snservice.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author:
 * @date: 2018/9/25
 * @time: 15:12
 * @description: To change this template use File | Settings | File Templates.
 */
public class DateUtil {

    public static final String DATE_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
    public static final String DATE_yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static final String DATE_yyyyMMdd = "yyyyMMdd";
    public static final String DATE_yyyy_MM_dd = "yyyy-MM-dd";
    public static final String FIRST_DAY_OF_MONTH = "yyyy-MM-01";


    //获取当前时间的前多少天
    public static String  getYYYYMMDD(int d){
        SimpleDateFormat sdf=new SimpleDateFormat(DATE_yyyy_MM_dd);
        Date date=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, d);
        date = calendar.getTime();
        System.out.println(sdf.format(date));
        return sdf.format(date);
    }




    //获取两个日期之间的所有日期
    public static List<String> getBetweenDates(String start, String end) {
        List<String> result = new ArrayList<String>();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date start_date = sdf.parse(start);
            Date end_date = sdf.parse(end);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start_date);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end_date);
            while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        Collections.reverse(result);
        return result;
    }

    //获取某天时间的前多少天
    public static String  getOneDayYYYYMMDD(String  eventDate,int d){
        SimpleDateFormat sdf=new SimpleDateFormat(DATE_yyyy_MM_dd);
        Date date = null;
        try {
            date = sdf.parse(eventDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, d);
        date = calendar.getTime();
        return sdf.format(date);
    }

    //获取某天时间的前多少天
    public static String  getOneYearYYYYMMDD(String  eventDate,int d){
        SimpleDateFormat sdf=new SimpleDateFormat(DATE_yyyy_MM_dd);
        Date date = null;
        try {
            date = sdf.parse(eventDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, d);
        date = calendar.getTime();
        return sdf.format(date);
    }

    /**
     * 转换时间
     *
     * @param pattern
     * @param d
     * @return
     * @throws ParseException
     */
    public static Date parseDate(String pattern, String d) throws ParseException {
        return new SimpleDateFormat(pattern).parse(d);
    }

    /**
     * 转换时间
     *
     * @param pattern
     * @param d
     * @return
     * @throws ParseException
     */
    public static String parseDateStr(String pattern, Date d) {
        return new SimpleDateFormat(pattern).format(d);
    }


    //两位年+两位月+两位日期+两位小时+两位分钟+两位秒
    public static String getYearMonthDayTimePoints() {
        Calendar cal = Calendar.getInstance();
        //当前年
        String year = String.valueOf(cal.get(Calendar.YEAR));
        year = year.substring(2, 4);
        //当前月
        String month = String.valueOf((cal.get(Calendar.MONTH)) + 1);
        if (month.length() == 1) {
            month = "0" + month;
        }
        //当前月的第几天：即当前日
        String day_of_month = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
        if (day_of_month.length() == 1) {
            day_of_month = "0" + day_of_month;
        }
        //当前时：HOUR_OF_DAY-24小时制；HOUR-12小时制
        String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
        if (hour.length() == 1) {
            hour = "0" + hour;
        }
        //当前分
        String minute = String.valueOf(cal.get(Calendar.MINUTE));
        if (minute.length() == 1) {
            minute = "0" + minute;
        }
        String second = String.valueOf(cal.get(Calendar.SECOND));
        if (second.length() == 1){
            second = "0" + second;
        }
        String date = year + month + day_of_month + hour + minute+second;
        return date;
    }

    /**
     * 查询当前时间
     * @return
     */
    public static String getNow(){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }
    public static String getNowYYYYMMDD(){
        return new SimpleDateFormat("yyyyMMdd").format(new Date());
    }

    /* //时间戳转换日期 */
    public static String stampToTime(String stamp) {
        String sd = "";
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_yyyy_MM_dd_HH_mm_ss);
        sd = sdf.format(new Date(Long.parseLong(stamp))); // 时间戳转换日期
        return sd;
    }

    /* //时间戳转换日期 */
    //yyyyMMdd
    public static String dateYYYYmmdd(String stamp) {
        String sd = "";
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_yyyyMMdd);
        sd = sdf.format(new Date(Long.parseLong(stamp))); // 时间戳转换日期
        return sd;
    }

    private List<Date> getBetweenDates(Date start, Date end) {
        List<Date> result = new ArrayList<Date>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd)) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    public static List<String> getBetweenStrDates(String startTime, String endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }

    public static List<String> getBetweenStrDatesYYYYMMDD(String startTime, String endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }

}
